$(function () {
    //申报公司对申报人的级联操作
    $("#wljsd_gs").jgrytree($("#wljsd_spr"), $("#wljsd_spr").val(),{
        onLoadSuccess:function(){
            $("#wljsd_gs").combotree('readonly',true);
        }
    });

    $("#wljsd_cggs").gsjgtree(1, {
        onLoadSuccess: function () {
            $("#wljsd_cggs").combotree('readonly', true);
        }
    })

    $("#wljsd_zbjs").unbind('click').bind('click', function () {
        if ($(this).attr("checked") != undefined && $(this).attr("checked") == "checked") {
            //TO-DO 赋值公司本部
            $("#wljsd_cggs").combotree('setValue', 1);
            $("#cgjsd_glqy").combobox('clear');
        } else {
            $("#wljsd_cggs").combotree('setValue', $("#wljsd_gs").combotree('getValue'));
            $("#cgjsd_glqy").combobox('clear');
        }
    });

    if ($("#wljsd_id").val()) {
        $("#wljsd_zbjs").unbind('click').bind('click', function () {
            return false;
        });
    }



    var wljsdId = $("#wljsd_id").val();
    if(wljsdId==""){
        wljsdId = -1;
    }

    //费用类别的切换
    $("#wljsd_gs").change(function () {
        $.post($.webPath() + '/cwgl/kmys/cyfyys/fylbcombobox', {'gsId': $("[name='wljsd.gs.id']").val()});
    });

    //物流结算单明细显示
    $.post($.webPath() + '/cwgl/kmys/cyfyys/fylbcombobox', {'gsId': $("[name='wljsd.gs.id']").val()},function (fsOptions) {
            $("#grid_wld").edatagrid({
                url:$.webPath() + "/jsgl/wljsd/wldSearchList?wljsdId="+wljsdId,
                fitColumns: true,
                singleSelect: true,
                height: 275,
                checkOnSelect: false,
                selectOnCheck: false,
                rownumbers: true,
                columns: [
                    [
                        {field:'id', title:'ID', hidden:true},
                        {field:'wljsd.id', title:'物流结算单ID', hidden:true},
                        {field:'cgd.id', title:'采购单ID', hidden:true},
                        {field:'sydw.id', title:'物流商ID', hidden:true},
                        {field:'sydw.qc', title:'物流商', width:40},
                        {field:'jfsl', title:'计费数量', width:25,
                            formatter:function(value){
                                var val = formatSlForHtml(value);
                                return val;
                            }},
                        {field:'dj', title:'单价', width:20,formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }},
                        {field:'je', title:'总金额', width:20,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field: 'fylb.id', editor: {type: 'combobox', options: {
                            data: fsOptions,
                            valueField: 'id',
                            textField: 'fykmjc',
                            multiple: false,
                            panelHeight: '100'
                        }
                        }, formatter: function (value) {
                            for (var i = 0; i < fsOptions.length; i++) {
                                if (fsOptions[i].id == value) return fsOptions[i].fykmjc;
                            }
                            return value;
                        }, title: '费用类别', width: 30},
                        {field: 'hdsl', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 4}},
                            title: '核定数量', width: 25,formatter:function(value){
                            var val = formatSlForHtml(value);
                            return val;
                        }},
                        {field: 'hddj', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 4}},
                            title: '核定单价', width: 25,formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }},
                        {field: 'hdje', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 2}},
                            title: '核定总金额', width: 25,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field: 'me', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 2}},
                            title: '总面额', width: 20,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field: 'kdse', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 2}},
                            title: '可抵税额', width: 20,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field: 'se', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 2}},
                            title: '税额', width: 20,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field: 'jsje', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 2}},
                            title: '净余额', width: 20,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field: 'ce', editor: {type: 'numberbox', options: {groupSeparator: ',', precision: 2}},
                            title: '差额', width: 20,formatter:function(value){
                            var val = formatJeForHtml2(value);
                            return val;
                        }},
                        {field:'bz', title:'备注', width:20}
                    ]
                ],
                onSelect: function (rowIndex, rowData) {
                    //获取每个单元格
                    $("#grid_wld").edatagrid('editRow', rowIndex);
                    var editors = $("#grid_wld").edatagrid('getEditors', rowIndex);
                    if (editors.length == 0)
                        return;
                    var fylbEditor = editors[0];//费用类别编辑框
                    var hdslEditor = editors[1];//核定数量编辑框
                    var hddjEditor = editors[2];//核定单价编辑框
                    var hdjeEditor = editors[3];//核定金额编辑框
                    var meEditor = editors[4];//面额编辑框
                    var kdseEditor = editors[5];//可抵税额编辑框
                    var seEditor = editors[6];//税额编辑框
                    var jsjeEditor = editors[7];//净额编辑框
                    var ceEditor = editors[8];//差额编辑框

                    hdslEditor.target.unbind("blur").bind('blur', function () {
                        setTimeout(function () {
                            $(hdslEditor.target).numberbox('setValue',$(hdslEditor.target).val());
                            $(hddjEditor.target).numberbox('setValue',$(hddjEditor.target).val());
                            $(hdjeEditor.target).numberbox('setValue',$(hdslEditor.target).numberbox('getValue')*$(hddjEditor.target).numberbox('getValue'));
                            $(ceEditor.target).numberbox('setValue',$(hdjeEditor.target).numberbox('getValue') - rowData.je);

                            var data = $("#grid_wld").edatagrid('getData');
                            var wljsd_me = 0.00, wljsd_jsje = 0.00,wljsd_se = 0.00 , wljsd_ce = 0.00;
                            for (var i = 0; i < data.rows.length; i++) {
                                if (i == rowIndex) {
                                    wljsd_me += parseFloat($(meEditor.target).numberbox('getValue'));
                                    wljsd_jsje += parseFloat($(jsjeEditor.target).numberbox('getValue'));
                                    wljsd_se += parseFloat($(seEditor.target).numberbox('getValue'));
                                    wljsd_ce += parseFloat($(ceEditor.target).numberbox('getValue'));
                                } else{
                                    wljsd_me += parseFloat(data.rows[i].me);
                                    wljsd_jsje += parseFloat(data.rows[i].jsje);
                                    wljsd_se += parseFloat(data.rows[i].se);
                                    wljsd_ce += parseFloat(data.rows[i].ce);
                                }
                            }
                            $("#wljsd_me").val(formatJeForHtml2(wljsd_me));
                            $("#wljsd_je").val(formatJeForHtml2(wljsd_jsje));
                            $("#wljsd_se").val(formatJeForHtml2(wljsd_se));
                            $("#wljsd_ce").val(formatJeForHtml2(wljsd_ce));

                            var ce = $(ceEditor.target).numberbox('getValue');
                            var zjbcdRows = $("#grid_zjbcd_wljsd").datagrid('getRows');
                            if(rowData['cgd.id'] != null){
                                $.post($.webPath() + '/cggl/cgdgl/cgdmxByCgdId',{'cgdId':rowData['cgd.id'],'ce':ce},function(data){
                                    $.each(data,function(i,obj){
                                        $.each(zjbcdRows,function(r,z){
                                            if (obj['id'] == z['cgdmx.id']) {
                                                $('#grid_zjbcd_wljsd').datagrid('deleteRow',r);
                                                return false;
                                            }
                                        })
                                        if (ce != 0) {
                                            $("#grid_zjbcd_wljsd").edatagrid('appendRow', {
                                                'lb.id': 281,
                                                'lb.z': '费用差异',
                                                'je': formatMoney(obj['je']),
                                                'cgd.id': rowData['cgd.id'],
                                                'cgd.cgdh': obj['cgd.cgdh'],
                                                'wzgg.code': obj['wzgg.code'],
                                                'wzgg.name': obj['wzgg.name'],
                                                'wzgg.ggxh': obj['wzgg.ggxh'],
                                                'wzgg.dw': obj['wzgg.dw'],
                                                'cgdmx.id': obj['id']
                                            });
                                            countSumWljsd();
                                        }
                                    });
                                })
                            }
                        },200);
                    });

                    hddjEditor.target.unbind("blur").bind('blur', function () {
                        setTimeout(function () {
                            $(hdslEditor.target).numberbox('setValue',$(hdslEditor.target).val());
                            $(hddjEditor.target).numberbox('setValue',$(hddjEditor.target).val());
                            $(hdjeEditor.target).numberbox('setValue',$(hdslEditor.target).numberbox('getValue')*$(hddjEditor.target).numberbox('getValue'));
                            $(ceEditor.target).numberbox('setValue',$(hdjeEditor.target).numberbox('getValue') - rowData.je);

                            var data = $("#grid_wld").edatagrid('getData');
                            var wljsd_me = 0.00, wljsd_jsje = 0.00,wljsd_se = 0.00 , wljsd_ce = 0.00;
                            for (var i = 0; i < data.rows.length; i++) {
                                if (i == rowIndex) {
                                    wljsd_me += parseFloat($(meEditor.target).numberbox('getValue'));
                                    wljsd_jsje += parseFloat($(jsjeEditor.target).numberbox('getValue'));
                                    wljsd_se += parseFloat($(seEditor.target).numberbox('getValue'));
                                    wljsd_ce += parseFloat($(ceEditor.target).numberbox('getValue'));
                                } else{
                                    wljsd_me += parseFloat(data.rows[i].me);
                                    wljsd_jsje += parseFloat(data.rows[i].jsje);
                                    wljsd_se += parseFloat(data.rows[i].se);
                                    wljsd_ce += parseFloat(data.rows[i].ce);
                                }
                            }
                            $("#wljsd_me").val(formatJeForHtml2(wljsd_me));
                            $("#wljsd_je").val(formatJeForHtml2(wljsd_jsje));
                            $("#wljsd_se").val(formatJeForHtml2(wljsd_se));
                            $("#wljsd_ce").val(formatJeForHtml2(wljsd_ce));

                            var ce = $(ceEditor.target).numberbox('getValue');
                            var zjbcdRows = $("#grid_zjbcd_wljsd").datagrid('getRows');
                            if(rowData['cgd.id'] != null){
                                $.post($.webPath() + '/cggl/cgdgl/cgdmxByCgdId',{'cgdId':rowData['cgd.id'],'ce':ce},function(data){
                                    $.each(data,function(i,obj){
                                        $.each(zjbcdRows,function(r,z){
                                            if (obj['id'] == z['cgdmx.id']) {
                                                $('#grid_zjbcd_wljsd').datagrid('deleteRow',r);
                                                return false;
                                            }
                                        })
                                        if (ce != 0) {
                                            $("#grid_zjbcd_wljsd").edatagrid('appendRow', {
                                                'lb.id': 281,
                                                'lb.z': '费用差异',
                                                'je': formatMoney(obj['je']),
                                                'cgd.id': rowData['cgd.id'],
                                                'cgd.cgdh': obj['cgd.cgdh'],
                                                'wzgg.code': obj['wzgg.code'],
                                                'wzgg.name': obj['wzgg.name'],
                                                'wzgg.ggxh': obj['wzgg.ggxh'],
                                                'wzgg.dw': obj['wzgg.dw'],
                                                'cgdmx.id': obj['id']
                                            });
                                            countSumWljsd();
                                        }
                                    });
                                })
                            }
                        },200);
                    });

                    hdjeEditor.target.unbind("blur").bind('blur', function () {
                        setTimeout(function () {
                            $(hdjeEditor.target).numberbox('setValue',$(hdjeEditor.target).val());
                            $(hdslEditor.target).numberbox('setValue',$(hdslEditor.target).val());
                            $(hddjEditor.target).numberbox('setValue',$(hdjeEditor.target).numberbox('getValue')/$(hdslEditor.target).numberbox('getValue'));
                            $(ceEditor.target).numberbox('setValue',$(hdjeEditor.target).numberbox('getValue') - rowData.je);

                            var data = $("#grid_wld").edatagrid('getData');
                            var wljsd_me = 0.00, wljsd_jsje = 0.00,wljsd_se = 0.00 , wljsd_ce = 0.00;
                            for (var i = 0; i < data.rows.length; i++) {
                                if (i == rowIndex) {
                                    wljsd_me += parseFloat($(meEditor.target).numberbox('getValue'));
                                    wljsd_jsje += parseFloat($(jsjeEditor.target).numberbox('getValue'));
                                    wljsd_se += parseFloat($(seEditor.target).numberbox('getValue'));
                                    wljsd_ce += parseFloat($(ceEditor.target).numberbox('getValue'));
                                } else{
                                    wljsd_me += parseFloat(data.rows[i].me);
                                    wljsd_jsje += parseFloat(data.rows[i].jsje);
                                    wljsd_se += parseFloat(data.rows[i].se);
                                    wljsd_ce += parseFloat(data.rows[i].ce);
                                }
                            }
                            $("#wljsd_me").val(formatJeForHtml2(wljsd_me));
                            $("#wljsd_je").val(formatJeForHtml2(wljsd_jsje));
                            $("#wljsd_se").val(formatJeForHtml2(wljsd_se));
                            $("#wljsd_ce").val(formatJeForHtml2(wljsd_ce));

                            var ce = $(ceEditor.target).numberbox('getValue');
                            var zjbcdRows = $("#grid_zjbcd_wljsd").datagrid('getRows');
                            if(rowData['cgd.id'] != null){
                                $.post($.webPath() + '/cggl/cgdgl/cgdmxByCgdId',{'cgdId':rowData['cgd.id'],'ce':ce},function(data){
                                    $.each(data,function(i,obj){
                                        $.each(zjbcdRows,function(r,z){
                                            if (obj['id'] == z['cgdmx.id']) {
                                                $('#grid_zjbcd_wljsd').datagrid('deleteRow',r);
                                                return false;
                                            }
                                        })
                                        if (ce != 0) {
                                            $("#grid_zjbcd_wljsd").edatagrid('appendRow', {
                                                'lb.id': 281,
                                                'lb.z': '费用差异',
                                                'je': formatMoney(obj['je']),
                                                'cgd.id': rowData['cgd.id'],
                                                'cgd.cgdh': obj['cgd.cgdh'],
                                                'wzgg.code': obj['wzgg.code'],
                                                'wzgg.name': obj['wzgg.name'],
                                                'wzgg.ggxh': obj['wzgg.ggxh'],
                                                'wzgg.dw': obj['wzgg.dw'],
                                                'cgdmx.id': obj['id']
                                            });
                                            countSumWljsd();
                                        }
                                    });
                                })
                            }
                        },200);
                    });

                    meEditor.target.unbind("blur").bind('blur', function () {
                        setTimeout(function () {
                            $(meEditor.target).numberbox('setValue',$(meEditor.target).val());
                            $(jsjeEditor.target).numberbox('setValue',$(meEditor.target).numberbox('getValue')-$(seEditor.target).numberbox('getValue'));
                            var data = $("#grid_wld").edatagrid('getData');
                            var wljsd_fpje = 0.00,wljsd_me = 0.00, wljsd_jsje = 0.00,wljsd_se = 0.00 , wljsd_ce = 0.00;
                            for (var i = 0; i < data.rows.length; i++) {
                                if (i == rowIndex) {
                                    wljsd_fpje += parseFloat($(meEditor.target).numberbox('getValue'));
                                    wljsd_me += parseFloat($(meEditor.target).numberbox('getValue'));
                                    wljsd_jsje += parseFloat($(jsjeEditor.target).numberbox('getValue'));
                                    wljsd_se += parseFloat($(seEditor.target).numberbox('getValue'));
                                    wljsd_ce += parseFloat($(ceEditor.target).numberbox('getValue'));
                                } else{
                                    wljsd_fpje += parseFloat(data.rows[i].me);
                                    wljsd_me += parseFloat(data.rows[i].me);
                                    wljsd_jsje += parseFloat(data.rows[i].jsje);
                                    wljsd_se += parseFloat(data.rows[i].se);
                                    wljsd_ce += parseFloat(data.rows[i].ce);
                                }
                            }
                            $("#wljsd_fpze").val(formatJeForHtml2(wljsd_fpje));
                            $("#wljsd_me").val(formatJeForHtml2(wljsd_me));
                            $("#wljsd_je").val(formatJeForHtml2(wljsd_jsje));
                            $("#wljsd_se").val(formatJeForHtml2(wljsd_se));
                            $("#wljsd_ce").val(formatJeForHtml2(wljsd_ce));
                        },200);
                    });

                    kdseEditor.target.unbind("blur").bind('blur', function () {
                        setTimeout(function () {
                            $(kdseEditor.target).numberbox('setValue',$(kdseEditor.target).val());
                            var data = $(fylbEditor.target).combobox("getData");
                            var value = $(fylbEditor.target).combobox("getValue");
                            var kdse = $(kdseEditor.target).numberbox('getValue');
                            var sl = $.grep(data,function(v,i){
                                if (v["id"] == value){
                                    $(seEditor.target).numberbox('setValue',kdse*v['sl']/100);
                                }
                            });
                            $(jsjeEditor.target).numberbox('setValue',$(meEditor.target).numberbox('getValue')-$(seEditor.target).numberbox('getValue'));
                            $(ceEditor.target).numberbox('setValue',$(hdjeEditor.target).numberbox('getValue') - rowData.je);

                            var data = $("#grid_wld").edatagrid('getData');
                            var wljsd_me = 0.00, wljsd_jsje = 0.00,wljsd_se = 0.00 , wljsd_ce = 0.00;
                            for (var i = 0; i < data.rows.length; i++) {
                                if (i == rowIndex) {
                                    wljsd_me += parseFloat($(meEditor.target).numberbox('getValue'));
                                    wljsd_jsje += parseFloat($(jsjeEditor.target).numberbox('getValue'));
                                    wljsd_se += parseFloat($(seEditor.target).numberbox('getValue'));
                                    wljsd_ce += parseFloat($(ceEditor.target).numberbox('getValue'));
                                } else{
                                    wljsd_me += parseFloat(data.rows[i].me);
                                    wljsd_jsje += parseFloat(data.rows[i].jsje);
                                    wljsd_se += parseFloat(data.rows[i].se);
                                    wljsd_ce += parseFloat(data.rows[i].ce);
                                }
                            }
                            $("#wljsd_me").val(formatJeForHtml2(wljsd_me));
                            $("#wljsd_je").val(formatJeForHtml2(wljsd_jsje));
                            $("#wljsd_se").val(formatJeForHtml2(wljsd_se));
                            $("#wljsd_ce").val(formatJeForHtml2(wljsd_ce));
                        },200);
                    });
                }
            })
        });

    //资金补差单
    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'zjbclb'}, function () {
        $("#grid_zjbcd_wljsd").edatagrid({
            url: $.webPath() + "/jsgl/zjbcd/zjbcdSearchWlList?wljsdId=" + wljsdId,
            fitColumns: true,
            singleSelect: true,
            fit: true,
            checkOnSelect: false,
            selectOnCheck: false,
            pagination: true,
            rownumbers: true,
            showFooter: true,
            columns: [
                [
                    {field:'id', title:'ID', hidden:true},
                    {field: 'lb.id', title:'类别ID', hidden:true},
                    {field: 'lb.z', title: '类别', width: 50},
                    {field: 'wzgg.code', title: '物资编码', width: 50},
                    {field: 'wzgg.name', title: '物资名称', width: 40},
                    {field: 'wzgg.ggxh', title: '规格型号', width: 40},
                    {field: 'wzgg.dw', title: '单位', width: 20},
                    {field: 'je', title: '总金额(元)', width: 50,
                        formatter: function (value) {
                            var val = formatMoney(value);
                            return val;
                        }
                    },
                    {field:'cgd.id', title:'采购单ID', hidden:true},
                    {field: 'cgd.cgdh', title: '采购单', width: 72}
                ]
            ],
            onSelect: function (rowIndex) {
                //获取每个单元格
                $("#grid_zjbcd_wljsd").edatagrid('editRow', rowIndex);
                var editors = $("#grid_zjbcd_wljsd").datagrid('getEditors', rowIndex);
                if (editors.length == 0)
                    return;
            },
            onLoadSuccess: function () {
                countSumWljsd();
            }
        });
    });

    //搜索物流单
    $("#btn_wld_search").click(function () {
        var wljsd_wls = $("#wljsd_glqy").combobox('getValue');
        var wljsd_gs = $("#wljsd_gs").combobox('getValue');
        if(wljsd_wls){
            searchwldwindow(grid_wld_searchWindowOKbtnHandler,wljsd_wls,wljsd_gs);
        }else {
        $('#form_wljsd').tabs('select', "结算单信息");
        $.messager.alert("提示", "请选择物流商!", "info");
        }
    });

    //客户关联纳税号
    $('#wljsd_glqy').combobox({
        onSelect:function(){
            var obj = $('#wljsd_glqy').combobox('getData');
            var id_val =  $('#wljsd_glqy').combobox('getValue');
            $.each(obj, function (i, val) {
                if(obj[i].id==id_val){
                    $('#wljsd_glqy_qyfr_nsh').val(obj[i].qyfr_nsh);
                }
            })
        }
    });

});

//查询物流单确定按钮回调函数
function grid_wld_searchWindowOKbtnHandler(data) {
    var hsjzh = 0;
    var bhsjzh = 0;
    var sezh = 0;

    $.each(data, function () {
        $("#grid_wld").edatagrid('appendRow', {
            'id': this.id,
            'cgd.id':this['cgd.id'],
            'sydw.id':this['sydw.id'],
            'sydw.qc': this['sydw.qc'],
            'fylb.id':this['fylb.id'],
            'fylb.fykmjc':this['fylb.fykmjc'],
            'jfsl':this.jfsl,
            'dj':this.dj,
            'je':this.je,
            'hdsl':this.jfsl,
            'hddj':this.dj,
            'hdje':this.je,
            'me':this.je,
            'kdse':formatJe2(this.je/(1+((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100))),
            'se':formatJe2(this.je/(1+((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100)))*((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100),
            'jsje':formatJe2(this.je - (this.je/(1+((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100)))*((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100)),
            'ce':0.00,
            'bz':this.bz
        });

        hsjzh += parseFloat(this.je);
        bhsjzh += this.je - (this.je/(1+((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100)))*((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100);
        sezh += (this.je/(1+((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100)))*((this['fylb.sl'] ==null?0:this['fylb.sl'] )/100);
    });

    $('#wljsd_me').val(formatJe2(hsjzh));
    $('#wljsd_fpze').val(formatJe2(hsjzh));
    $('#wljsd_je').val(formatJe2(bhsjzh));
    $('#wljsd_se').val(formatJe2(sezh));
}

//合计计算
function countSumWljsd() {
    var sumje = 0;
    var data = $("#grid_zjbcd_wljsd").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var je = parseFloat(data.rows[i]["je"]);
        je = isNaN(je) ? 0 : je;
        sumje += je;
    }
    addFooter(sumje);
}

function addFooter(sumje) {
    var footer = null;
    footer = [
        {"lb.z": "合计", "je": formatMoney(sumje)}
    ];
    if (footer != null)
        $("#grid_zjbcd_wljsd").edatagrid('reloadFooter', footer);
}

